package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ProductConverter;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.productStockMapper;
import com.goods.business.service.ProductService;
import com.goods.common.error.SystemCodeEnum;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * projectName: goods
 *
 * @author: ysc
 * time: 2022/12/3 21:57 周六
 * description:
 */
@Service
public class ProductServiceImpl implements ProductService {


    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private productStockMapper productStockMapper;


    //物资资料列表
    @Override
    public PageVO<ProductVO> findProductList(Integer pageNum, Integer pageSize, ProductVO productVO) {
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        //1.状态
        if (productVO.getStatus() != null){
            criteria.andEqualTo("status",productVO.getStatus());
        }
        //2.判断一级分类id 1 2 3 需要导读进行整理zhuanweilist
        if (productVO.getThreeCategoryId() != null){
            criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId())
                    .andEqualTo("twoCategoryId",productVO.getTwoCategoryId())
                    .andEqualTo("threeCategoryId",productVO.getThreeCategoryId());
            List<Product> products = productMapper.selectByExample(example);
            List<ProductVO> categoryVOS = ProductConverter.converterToVOList(products);
            PageInfo<Product> info =new PageInfo<>(products);
            return new PageVO<>(info.getTotal(), categoryVOS);
        }
        //3.判断二级分类
        if (productVO.getTwoCategoryId() !=null){
            criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId())
                    .andEqualTo("twoCategoryId",productVO.getTwoCategoryId());
            List<Product> products = productMapper.selectByExample(example);
            List<ProductVO> categoryVOs = ProductConverter.converterToVOList(products);
            PageInfo<Product> info =new PageInfo<>(products);
            return new PageVO<>(info.getTotal(), categoryVOs);
        }
        //4.判断1级分类
        if (productVO.getOneCategoryId() !=null){
            criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId());
            List<Product> products = productMapper.selectByExample(example);
            List<ProductVO> categoryVOs = ProductConverter.converterToVOList(products);
            PageInfo<Product> info =new PageInfo<>(products);
            return new PageVO<>(info.getTotal(), categoryVOs);
        }

        //排序
        example.setOrderByClause("sort asc");
        //判断名字
        if (productVO.getName() != null && !"".equals(productVO.getName())){
            criteria.andLike("name","%"+productVO.getName()+"%");
        }

        List<Product> products = productMapper.selectByExample(example);
        List<ProductVO> productVOS = ProductConverter.converterToVOList(products);
        PageInfo<Product> info =new PageInfo<>(products);
        return new PageVO<>(info.getTotal(), productVOS);
    }

    //分类列表
    @Override
    public void categoryTree() {

    }


    //添加
    @Override
    public void add(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        //获取分类id
        @NotNull(message = "分类不能为空") Long[] categoryKeys = productVO.getCategoryKeys();
        //获取分类钥匙
        if (categoryKeys.length == 3){
            product.setOneCategoryId(categoryKeys[0]);
            product.setTwoCategoryId(categoryKeys[1]);
            product.setThreeCategoryId(categoryKeys[2]);
        }
        //设置添加未审核
        product.setStatus(2);
        product.setPNum(UUID.randomUUID().toString().substring(0,32));
        productMapper.insert(product);
    }


    ////移入回收站
    @Override
    public void remove(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(1);
        productMapper.updateByPrimaryKeySelective(product);
    }


    //拿出回收站
    @Override
    public void back(Long id) {
        Product product =new Product();
        product.setId(id);
        Product product1 = productMapper.selectByPrimaryKey(product);
        product1.setStatus(0);
        productMapper.updateByPrimaryKeySelective(product1);
    }


    //通过验证
    @Override
    public void publish(Long id) {
        Product p =new Product();
        p.setId(id);
        Product product = productMapper.selectByPrimaryKey(p);
        product.setStatus(0);
        productMapper.updateByPrimaryKeySelective(product);
    }


    //删除
    @Override
    public void delete(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        productMapper.deleteByPrimaryKey(product);
    }


    //库存列表（左侧图使用）
    @Override
    public List<ProductStockVO> findAllStocks(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductStockVO> productStockVOList =productStockMapper.findAllStocks();
        return productStockVOList;
    }

    //库存列表（右侧图使用）
    @Override
    public PageVO<ProductStockVO> findProductStocks(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductStockVO> allStocks = productStockMapper.findAllStocks();
        PageInfo<ProductStockVO> info =new PageInfo<>(allStocks);
        return new PageVO<>(info.getTotal(),allStocks);
    }

}
